package com.wu.search.service.Impl;

import com.wu.search.cache.SuggestWordCache;
import com.wu.search.constant.ElasticConstants;
import com.wu.search.schedule.SuggestWordBaseSchedule;
import com.wu.search.service.SearchService;
import com.wu.search.util.PinYinUtils;
import com.wu.search.util.RestHighLevelClientUtils;
import com.wu.search.vo.SuggestResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.mustache.MultiSearchTemplateRequest;
import org.elasticsearch.script.mustache.MultiSearchTemplateResponse;
import org.elasticsearch.script.mustache.SearchTemplateRequest;
import org.elasticsearch.search.suggest.Suggest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Cousin
 * @date 2025/4/9
 * @Description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SearchServiceImpl implements SearchService {
    private final RestHighLevelClient restHighLevelClient;
    @Override
    public List<SuggestResp> getSuggestions(String query) {
        if (StringUtils.isEmpty(query)){
            return SuggestWordCache.getInstance().getSuggestRespList();
        }
        String index = ElasticConstants.SUGGEST_INDEX.getCode();
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", query);
        Map<String, Object> pyParams = new HashMap<>();
        pyParams.put("keyword", query);
        try {
            pyParams.put("keyword", PinYinUtils.getPinyin(query));
        } catch (Exception e) {
        log.warn("===BadHanyuPinyinOutputFormatCombination keyword={} \n error = {}",query, e.toString());
    }
        SearchTemplateRequest zwRequest = new SearchTemplateRequest();
        zwRequest.setRequest(new SearchRequest(index));
        zwRequest.setScriptType(ScriptType.STORED);
        zwRequest.setScript(ElasticConstants.AS_SUGGEST_TEMPLATE.getCode());
        zwRequest.setScriptParams(params);

        SearchTemplateRequest pyRequest = new SearchTemplateRequest();
        pyRequest.setRequest(new SearchRequest(index));
        pyRequest.setScriptType(ScriptType.STORED);
        pyRequest.setScript(ElasticConstants.AS_SUGGEST_FUZZY_TEMPLATE.getCode());
        pyRequest.setScriptParams(pyParams);

        MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();

        multiRequest.add(zwRequest);
        multiRequest.add(pyRequest);

        MultiSearchTemplateResponse multiSearchTemplateResponse = RestHighLevelClientUtils.getMultiSuggestsResponse(restHighLevelClient, multiRequest);

        if (multiSearchTemplateResponse != null) {
            try {
                return getSuggests(multiSearchTemplateResponse);
            } catch (Exception e) {
                log.warn("====解析猜想=e=====", e);
            }
        }
        return new ArrayList<>();
    }
    private List<SuggestResp> getSuggests(MultiSearchTemplateResponse multiSearchTemplateResponse) {
        MultiSearchTemplateResponse.Item[] responses = multiSearchTemplateResponse.getResponses();
        MultiSearchTemplateResponse.Item item = responses[0];
        List<SuggestResp> zwSuggests = getSuggests(item);
        if (ObjectUtils.isNotEmpty(zwSuggests)) {
            return zwSuggests;
        }

        MultiSearchTemplateResponse.Item item2 = responses[1];
        return getSuggests(item2);
    }
    private List<SuggestResp> getSuggests(MultiSearchTemplateResponse.Item item) {
        SearchResponse response = item.getResponse().getResponse();
        return setSearchResponse(response);
    }
    public List<SuggestResp> setSearchResponse(SearchResponse searchResponse) {
        List<SuggestResp> resultSuggests = new ArrayList<>();
        Suggest suggest = searchResponse.getSuggest();

        String suggestStr = suggest.toString();

        return resultSuggests;
    }
}
